package org.jeecg.modules.exTender.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.exBond.entity.ExBondPaysituation;
import org.jeecg.modules.exBond.service.IExBondPaysituationService;
import org.jeecg.modules.exOperationRecord.entity.ExOperationRecord;
import org.jeecg.modules.exOperationRecord.service.IExOperationRecordService;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpert;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpertStage;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertMapper;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertStageMapper;
import org.jeecg.modules.exTender.entity.ExTenderList;
import org.jeecg.modules.exTender.mapper.ExTenderListMapper;
import org.jeecg.modules.exTender.service.IExTenderListService;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.entity.ExZbwjBidEvaluationConfig;
import org.jeecg.modules.exZbwjBidEvaluationConfig.domain.entity.ExZbwjBidEvaluationItem;
import org.jeecg.modules.exZbwjBidEvaluationConfig.mapper.ExZbwjBidEvaluationItemMapper;
import org.jeecg.modules.exZbwjBidEvaluationConfig.service.IExZbwjBidEvaluationConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 投标人管理
 * @Author: jeecg-boot
 * @Date: 2024-11-09
 * @Version: V1.0
 */
@Service
public class ExTenderListServiceImpl extends ServiceImpl<ExTenderListMapper, ExTenderList> implements IExTenderListService {

    @Autowired
    private ExPingbiaoExpertMapper exPingbiaoExpertMapper;
    @Autowired
    private ExPingbiaoExpertStageMapper exPingbiaoExpertStageMapper;
    @Autowired
    private ExZbwjBidEvaluationItemMapper evaluationItemMapper;
    @Autowired
    private IExOperationRecordService exOperationRecordService;
    /**
     * 保证金
     */
    @Autowired
    private IExBondPaysituationService exBondPaysituationService;
    @Override
    public List<ExTenderList> queryBySectionId(String bidSectionId) {
        return getBaseMapper().selectList(new LambdaQueryWrapper<ExTenderList>().eq(ExTenderList::getBidSectionId, bidSectionId));
    }

    @Override
    public List<ExTenderList> queryBySectionIdAndExpertId(String bidSectionId, String realBusinessId, String expertId) {
        List<ExTenderList> tenderListList = getBaseMapper().selectList(new LambdaQueryWrapper<ExTenderList>().eq(ExTenderList::getBidSectionId, bidSectionId));
        ExPingbiaoExpertStage exPingbiaoExpertStage = exPingbiaoExpertStageMapper.selectBySectionId(bidSectionId);
        Map<String, ExPingbiaoExpert> pingbiaoExpertMap = Maps.newHashMap();
        if (null != exPingbiaoExpertStage) {
            if(StringUtils.isEmpty(realBusinessId)){
                ExZbwjBidEvaluationItem exZbwjBidEvaluationItem = evaluationItemMapper.selectById(exPingbiaoExpertStage.getBusinessId());
                realBusinessId = exZbwjBidEvaluationItem.getBusinessId();
            }
            List<ExPingbiaoExpert> pingbiaoExpertList = exPingbiaoExpertMapper.selectByExpertIdAndRealBusinessIdList(expertId, com.google.common.collect.Lists.newArrayList(realBusinessId));
            pingbiaoExpertMap = pingbiaoExpertList.stream().collect(Collectors.toMap(ExPingbiaoExpert::getTenderListId, Function.identity()));
        }
        for (ExTenderList exTenderList : tenderListList) {
            if(pingbiaoExpertMap.containsKey(exTenderList.getId())){
                exTenderList.setCompleteStatus("1".equals(pingbiaoExpertMap.get(exTenderList.getId()).getCompleteStatus()));
            }
        }
        return tenderListList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInfo(ExTenderList exTenderList) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        exTenderList.setBidderName(sysUser.getRealname());
        exTenderList.setBidderUserId(sysUser.getId());
        this.save(exTenderList);
        ExBondPaysituation exBondPaysituation = new ExBondPaysituation();
        exBondPaysituation.setBidSectionId(exTenderList.getBidSectionId());
        exBondPaysituation.setBidderName(exTenderList.getBidderName());
        exBondPaysituation.setBidderOrgCode(exTenderList.getBidderOrgCode());
        exBondPaysituation.setMarginPayStatus("0");
        exBondPaysituation.setIfBind(0);
        exBondPaysituationService.save(exBondPaysituation);
        ExOperationRecord exOperationRecord = new ExOperationRecord();
        exOperationRecord.setEntryId(exTenderList.getId());
        exOperationRecord.setModule("exTenderList");
        exOperationRecord.setOperateTime(new Date());
        exOperationRecord.setOperateResult("1");//1 通过 2驳回
        exOperationRecord.setOperateOpinion("系统默认通过");
        exOperationRecord.setCategory("1");
        exOperationRecord.setOperator(sysUser.getRealname());
        exOperationRecordService.save(exOperationRecord);
    }
}
